home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11370 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.mindspring.com!usenet
  2. From: rudd@mindspring.com (Justin Rudd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Null Pointer Assignment
  5. Date: Thu, 14 Mar 1996 05:16:51 GMT
  6. Organization: MindSpring Enterprises
  7. Message-ID: <4i8a68$gfr@B1FF.mindspring.com>
  8. References: <4i84kd$1lh4@saba.info.ucla.edu>
  9. Reply-To: rudd@mindspring.com
  10. NNTP-Posting-Host: rudd.mindspring.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. mael1@ucla.edu (Matt Waggoner) wrote:
  14.  
  15. >I have a class called City that contains a private data member, char *name.  
  16. >The constructor receives an argument called char *s.  The constructor looks 
  17. >like this:
  18.  
  19. >City::City(char *s)
  20. >{
  21. >    name = new char(strlen(s));
  22. >    name = s;
  23. >}
  24.  
  25. OK...first off you have name pointing to a temporary variable s.  Do
  26. this instead.
  27.  
  28. City::City( char* s )
  29. {
  30.     name = new char(strlen(s)+1];     //+1 for \0
  31.     strcpy(name,s);
  32. }
  33.  
  34. This should do it for ya....if not...flame me ;-)
  35.  
  36. >The program works fine, but I get a NULL POINTER ASSIGNMENT whenever it 
  37. >terminates.  What the HELL causes this, and how do I get rid of it?  A quick 
  38. >answer would be good, I'm under a bit of time pressure... thanks in advance.
  39.  
  40.  
  41.